//
//  BuyHouseViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "BuyHouseViewController.h"

#import "BuyHouseCell.h"
#import "RentHouseCell.h"
#import "ZFDetViewController.h"
#import "BHDetViewController.h"
#import "SearchViewController.h"
#import "WTImageScroll.h"

#import "BHAreaView.h"
#import "BHPriceView.h"
#import "BHYTView.h"
#import "BHMoreView.h"

#import "RHAreaView.h"
#import "RHPriceView.h"
#import "RHYTView.h"
#import "RHMoreView.h"

#import "LoginViewController.h"
#import "NavViewController.h"
@interface BuyHouseViewController ()<UITableViewDelegate,UITableViewDataSource,BMKLocationServiceDelegate,BHAreaViewDelegate,BHPriceViewDelegate,BHYTViewDelegate,BHMoreViewDelegate,RHAreaViewDelegate,RHPriceViewDelegate,RHYTViewDelegate,RHMoreViewDelegate>{
    UILabel *labLine;//红线
    NSMutableArray *array;//买房推荐数据
    NSMutableArray *array2;//买房不推荐数据
    NSMutableArray *datas;//租房数据
    BMKLocationService *location;
    CGFloat lat;//
    CGFloat lng;//
    NSInteger pageNo;
    
    BHAreaView *areaView;
    BHPriceView *priceView;
    BHYTView *ytView;
    BHMoreView *moreView;
    
    RHAreaView *rhareaView;
    RHPriceView *rhpriceView;
    RHYTView *rhytView;
    RHMoreView *rhmoreView;
    
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;
    UIButton *btn4;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *dic;//标示
@property (nonatomic,strong)NSMutableArray *imagesUrl;
@property (nonatomic,strong)NSMutableDictionary *bhDic;
@property (nonatomic,strong)NSMutableDictionary *bhText;//买房
@property (nonatomic,strong)NSMutableDictionary *rhDic;//出租


@end

@implementation BuyHouseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadNav];
    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _dic = [NSMutableDictionary dictionary];
    array = [NSMutableArray array];
    array2 = [NSMutableArray array];
    datas = [NSMutableArray array];
    _imagesUrl = [NSMutableArray array];
    _bhDic = [NSMutableDictionary dictionary];
    _bhText = [NSMutableDictionary dictionary];
    _rhDic = [NSMutableDictionary dictionary];
    [_dic setObject:@"aaa" forKey:@"tag"];
    pageNo = 1;
    
    [_bhDic setObject:@"" forKey:@"area"];
    [_bhDic setObject:@"" forKey:@"price"];
    [_bhDic setObject:@"" forKey:@"yt"];
    [_bhDic setObject:@"" forKey:@"more"];
    
    [_bhText setObject:@"区域" forKey:@"area"];
    [_bhText setObject:@"价格" forKey:@"price"];
    [_bhText setObject:@"用途" forKey:@"yt"];
    [_bhText setObject:@"更多" forKey:@"more"];
    
    [self loadUI];
    
    [self loadBanner:@"/App/Bridalchamber/banner"];
    [self loadMap];
    //创建刷新控件
    [self loadRefresh];
    
}
//定位
- (void)loadMap{
    location = [[BMKLocationService alloc] init];
    [location startUserLocationService];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    location.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    location.delegate = nil;
}

#pragma -mark BMKLocationServiceDelegate
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    lat = userLocation.location.coordinate.latitude;
    lng = userLocation.location.coordinate.longitude;
    [location stopUserLocationService];
    
    //获取城市
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        if (placemarks > 0) {
            CLPlacemark *mark = [placemarks objectAtIndex:0];
            if (mark) {
                
                NSString *city = mark.locality;
                //地区
                [user setObject:city forKey:@"city"];
                
            }
        }
    }];
    
//    [self loadData];
//    [self loadData2];
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UIButton *btnL = [UIButton buttonWithType:UIButtonTypeCustom];
    btnL.frame = CGRectMake(ScreenWidth/2-50, 30, 50, 30);
    [btnL setTitle:@"买房" forState:UIControlStateNormal];
    [btnL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnL addTarget:self action:@selector(clickL) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnL];
    
    UIButton *btnR = [UIButton buttonWithType:UIButtonTypeCustom];
    btnR.frame = CGRectMake(ScreenWidth/2+10, 30, 50, 30);
    [btnR setTitle:@"租房" forState:UIControlStateNormal];
    [btnR setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnR addTarget:self action:@selector(clickR) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnR];
    
    labLine = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2-45, 55, 40, 4)];
    labLine.backgroundColor = [UIColor redColor];
    labLine.layer.cornerRadius = 5.0;
    [view addSubview:labLine];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
    
    UIButton *btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSearch.frame = CGRectMake(ScreenWidth-40, 24, 40, 40);
    [btnSearch setImage:[UIImage imageNamed:@"搜索-(1)"] forState:UIControlStateNormal];
    [btnSearch addTarget:self action:@selector(clickSearch) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btnSearch];
    
}

- (void)clickL{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake(ScreenWidth/2-45, 55, 40, 4);
    }];
    [_dic setObject:@"aaa" forKey:@"tag"];
    [rhareaView removeFromSuperview];
    [rhpriceView removeFromSuperview];
    [rhytView removeFromSuperview];
    [rhmoreView removeFromSuperview];
    [_bhText setObject:@"区域" forKey:@"area"];
    [_bhText setObject:@"价格" forKey:@"price"];
    [_bhText setObject:@"用途" forKey:@"yt"];
    [_bhText setObject:@"更多" forKey:@"more"];
    [self loadBanner:@"/App/Bridalchamber/banner"];
    if (datas.count <= 0) {
        [_tableView.mj_header beginRefreshing];
    }else{
        [_tableView reloadData];
    }
}

- (void)clickR{
    [UIView animateWithDuration:0.3 animations:^{
        labLine.frame = CGRectMake(ScreenWidth/2+15, 55, 40, 4);
    }];
    [_dic setObject:@"sss" forKey:@"tag"];
    [areaView removeFromSuperview];
    [priceView removeFromSuperview];
    [ytView removeFromSuperview];
    [moreView removeFromSuperview];
    [_bhText setObject:@"区域" forKey:@"area"];
    [_bhText setObject:@"价格" forKey:@"price"];
    [_bhText setObject:@"户型" forKey:@"yt"];
    [_bhText setObject:@"更多" forKey:@"more"];
    [self loadBanner:@"/App/Rentout/rentbanner"];
    if (datas.count <= 0) {
       [_tableView.mj_header beginRefreshing];
    }else{
        [_tableView reloadData];
    }
    
}

- (void)clickSearch{
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
//
- (void)loadBanner:(NSString *)urlfoot{
    [_imagesUrl removeAllObjects];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",urlHead,urlfoot];
    NSLog(@"url:%@",url);
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"BANNERres:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            for (NSDictionary *dic in arr) {
                NSString *img = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"banner"]];
                [_imagesUrl addObject:img];
            }
            UIView *v = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 0, ScreenWidth, 171) andImageArray:_imagesUrl andBtnClick:^(NSInteger tagValue) {
                
            }];
            _tableView.tableHeaderView = v;
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}
//
- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-49-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        return 3;
    }
    return 1;
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (section == 0) {
            return 0;
        }else if (section == 1){
            return array.count;
        }else{
            return array2.count;
        }
    }else{
        return datas.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (indexPath.section == 0) {
            static NSString *cellid = @"cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            }
            
            return cell;
        }else if (indexPath.section == 1){
            static NSString *cellId = @"Cell1";
            BuyHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"BuyHouseCell" owner:nil options:nil] lastObject];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.labTit1.layer.cornerRadius = 5.0;
            cell.labTit1.layer.borderWidth = 0.5;
            cell.labTit1.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
            
            cell.labTit2.layer.cornerRadius = 5.0;
            cell.labTit2.layer.borderWidth = 0.5;
            cell.labTit2.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
            NSDictionary *dic = array[indexPath.row];
            cell.labName.text = [dic objectForKey:@"title"];
            cell.labDet.text = [dic objectForKey:@"address"];
            
            cell.labPrice.text = [NSString stringWithFormat:@"%li元/㎡",[[dic objectForKey:@"price"] integerValue]];
            NSString *state = [dic objectForKey:@"labelling"];
            NSArray *arr = [state componentsSeparatedByString:@"|"];
            NSString *str1 = arr[0];
            NSString *str2 = [arr lastObject];
            if ([str1 isEqualToString:str2]) {
                cell.labTit1.text = str1;
                cell.labTit2.hidden = YES;
            }else{
                cell.labTit1.text = str1;
                cell.labTit2.text = str2;
            }
            
            cell.labKM.text = [NSString stringWithFormat:@"%likm",[[dic objectForKey:@"km"] integerValue]];
            cell.labMJ.text = [NSString stringWithFormat:@"建面%@㎡",[dic objectForKey:@"qujian"]];
            //图片数组
            NSString *pic = [dic objectForKey:@"homepic"];
            NSArray *imgs = [pic componentsSeparatedByString:@"|"];
            NSString *image0 = [imgs objectAtIndex:0];
            NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],image0];
            [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"timg-(6)"]];
            
            return cell;
        }else{
            static NSString *cellId = @"Cell1";
            BuyHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"BuyHouseCell" owner:nil options:nil] lastObject];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.labTit1.layer.cornerRadius = 5.0;
            cell.labTit1.layer.borderWidth = 0.5;
            cell.labTit1.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
            
            cell.labTit2.layer.cornerRadius = 5.0;
            cell.labTit2.layer.borderWidth = 0.5;
            cell.labTit2.layer.borderColor = COLOR_RGB(17, 100, 253, 1).CGColor;
            NSDictionary *dic = array2[indexPath.row];
            cell.labName.text = [dic objectForKey:@"title"];
            cell.labDet.text = [dic objectForKey:@"address"];
            
            cell.labPrice.text = [NSString stringWithFormat:@"%li元/㎡",[[dic objectForKey:@"price"] integerValue]];
            NSString *state = [dic objectForKey:@"labelling"];
            NSArray *arr = [state componentsSeparatedByString:@"|"];
            NSString *str1 = arr[0];
            NSString *str2 = [arr lastObject];
            if ([str1 isEqualToString:str2]) {
                cell.labTit1.text = str1;
                cell.labTit2.hidden = YES;
            }else{
                cell.labTit1.text = str1;
                cell.labTit2.text = str2;
            }
            
            cell.labKM.text = [NSString stringWithFormat:@"%likm",[[dic objectForKey:@"km"] integerValue]];
            cell.labMJ.text = [NSString stringWithFormat:@"建面%@㎡",[dic objectForKey:@"qujian"]];
            //图片数组
            NSString *pic = [dic objectForKey:@"homepic"];
            NSArray *imgs = [pic componentsSeparatedByString:@"|"];
            NSString *image0 = [imgs objectAtIndex:0];
            NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],image0];
            [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"timg-(6)"]];
            
            return cell;
        }
        
    }else{//出租房
        static NSString *cellId = @"Cell2";
        RentHouseCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"RentHouseCell" owner:nil options:nil] lastObject];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (datas.count > 0) {
            NSDictionary *dic = datas[indexPath.row];
            //图片数组
            NSString *pic = [dic objectForKey:@"pic"];
            NSArray *imgs = [pic componentsSeparatedByString:@"|"];
            NSString *image0 = [imgs objectAtIndex:0];
            NSString *imgUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],image0];
            [cell.images sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"timg-(6)"]];
            
            cell.labTitle.text = [dic objectForKey:@"title"];
            cell.labArea.text = [dic objectForKey:@"community"];
            cell.labHX.text = [dic objectForKey:@"room"];
            cell.labRentState.text = [dic objectForKey:@"mode"];
            NSString *price = [NSString stringWithFormat:@"%li元/月",[[dic objectForKey:@"mnthly"] integerValue]];
            cell.labPrice.text = price;
            NSString *juli = [NSString stringWithFormat:@"%likm",[[dic objectForKey:@"km"] integerValue]];
            cell.labJuLi.text = juli;
        }
        
        return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![user objectForKey:@"userid"]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        NavViewController *nav = [[NavViewController alloc] initWithRootViewController:login];
        nav.navigationBarHidden = YES;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (indexPath.section == 1) {
            NSDictionary *dic = array[indexPath.row];
            NSString *hId = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
            NSString *homepic = [dic objectForKey:@"homepic"];
            NSArray *arr = [homepic componentsSeparatedByString:@"|"];
            NSMutableArray *Images = [NSMutableArray array];
            for (NSString *str in arr) {
                NSString *imgs = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],str];
                [Images addObject:imgs];
            }
            BHDetViewController *det = [[BHDetViewController alloc] init];
            det.hid = hId;
            det.images = Images;
            [self.navigationController pushViewController:det animated:YES];
        }else if (indexPath.section == 2){
            NSDictionary *dic = array2[indexPath.row];
            NSString *hId = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
            NSString *homepic = [dic objectForKey:@"homepic"];
            NSArray *arr = [homepic componentsSeparatedByString:@"|"];
            NSMutableArray *Images = [NSMutableArray array];
            for (NSString *str in arr) {
                NSString *imgs = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],str];
                [Images addObject:imgs];
            }
            BHDetViewController *det = [[BHDetViewController alloc] init];
            det.hid = hId;
            det.images = Images;
            [self.navigationController pushViewController:det animated:YES];
        }
        
    }else{
        NSDictionary *op = datas[indexPath.row];
        NSString *houseId = [NSString stringWithFormat:@"%li",[[op objectForKey:@"id"] integerValue]];
        ZFDetViewController *det = [[ZFDetViewController alloc] init];
        det.hID = houseId;
        det.lat = [NSString stringWithFormat:@"%f",lat];
        det.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:det animated:YES];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (indexPath.section == 0) {
            return 0;
        }
        return 110.0;
    }else{
        return 116.0;
    }
}
//头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        UILabel *labL = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 4, 20)];
        labL.backgroundColor = COLOR_RGB(17, 100, 253, 1);
        [view addSubview:labL];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(36, 10, 100, 20)];
        lab.text = @"推荐房源";
        [view addSubview:lab];
        
        if (array.count == 0) {
            
            return nil;
        }else{
            
            return view;

        }
        
    }else if (section == 2){
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
        UILabel *labL = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 4, 20)];
        labL.backgroundColor = COLOR_RGB(17, 100, 253, 1);
        [view addSubview:labL];
        
        UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(36, 10, 100, 20)];
        lab.text = @"附近房源";
        [view addSubview:lab];
        
        return view;
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    lab.backgroundColor = [UIColor grayColor];
    [view addSubview:lab];
    
    btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(0, 0, ScreenWidth/4, 40);
    [btn1 setTitle:[_bhText objectForKey:@"area"] forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn1 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn1 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn1.imageView.image.size.width, 0, btn1.imageView.image.size.width)];
    [btn1 setImageEdgeInsets:UIEdgeInsetsMake(0, btn1.titleLabel.frame.size.width+5, 0, -btn1.titleLabel.frame.size.width)];
    [btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn1];
    
    btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth/4, 0, ScreenWidth/4, 40);
    [btn2 setTitle:[_bhText objectForKey:@"price"] forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn2 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn2 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn2.imageView.image.size.width, 0, btn2.imageView.image.size.width)];
    [btn2 setImageEdgeInsets:UIEdgeInsetsMake(0, btn2.titleLabel.frame.size.width+5, 0, -btn2.titleLabel.frame.size.width)];
    [btn2 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn2];
    
    btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = CGRectMake(ScreenWidth/2, 0, ScreenWidth/4, 40);
    [btn3 setTitle:[_bhText objectForKey:@"yt"] forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn3 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn3.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn3 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn3.imageView.image.size.width, 0, btn3.imageView.image.size.width)];
    [btn3 setImageEdgeInsets:UIEdgeInsetsMake(0, btn3.titleLabel.frame.size.width+5, 0, -btn3.titleLabel.frame.size.width)];
    [btn3 addTarget:self action:@selector(click3) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn3];
    
    btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn4.frame = CGRectMake(ScreenWidth/4*3, 0, ScreenWidth/4, 40);
    [btn4 setTitle:[_bhText objectForKey:@"more"] forState:UIControlStateNormal];
    [btn4 setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn4 setImage:[UIImage imageNamed:@"多边形-2"] forState:UIControlStateNormal];
    btn4.titleLabel.font = [UIFont systemFontOfSize:15.0];
    //设置文字在左，图片在右
    [btn4 setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn4.imageView.image.size.width, 0, btn4.imageView.image.size.width)];
    [btn4 setImageEdgeInsets:UIEdgeInsetsMake(0, btn4.titleLabel.frame.size.width+5, 0, -btn4.titleLabel.frame.size.width)];
    [btn4 addTarget:self action:@selector(click4) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn4];
    
    return view;
}
//按钮点击事件//171+64+40
- (void)click1{
    [_tableView setContentOffset:CGPointMake(0, 275) animated:YES];
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (![areaView superview]) {
            areaView = [[BHAreaView alloc] init];
            areaView.delegate = self;
            [self.view addSubview:areaView];
            [areaView show];
        }
        [priceView removeFromSuperview];
        [ytView removeFromSuperview];
        [moreView removeFromSuperview];
    }else{
        if (![rhareaView superview]) {
            rhareaView = [[RHAreaView alloc] init];
            rhareaView.delegate = self;
            [self.view addSubview:rhareaView];
            [rhareaView show];
        }
        
        [rhpriceView removeFromSuperview];
        [rhytView removeFromSuperview];
        [rhmoreView removeFromSuperview];
    }
    self.tabBarController.tabBar.hidden = YES;
}

- (void)click2{
    [_tableView setContentOffset:CGPointMake(0, 275) animated:YES];
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (![priceView superview]) {
            priceView = [[BHPriceView alloc] init];
            priceView.delegate = self;
            [self.view addSubview:priceView];
            [priceView show];
        }
        [areaView removeFromSuperview];
        [ytView removeFromSuperview];
        [moreView removeFromSuperview];
    }else{
        if (![rhpriceView superview]) {
            rhpriceView = [[RHPriceView alloc] init];
            rhpriceView.delegate = self;
            [self.view addSubview:rhpriceView];
            [rhpriceView show];
        }
        [rhareaView removeFromSuperview];
        [rhytView removeFromSuperview];
        [rhmoreView removeFromSuperview];
    }
    self.tabBarController.tabBar.hidden = YES;
}

- (void)click3{
    [_tableView setContentOffset:CGPointMake(0, 275) animated:YES];
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (![ytView superview]) {
            ytView = [[BHYTView alloc] init];
            ytView.delegate = self;
            [self.view addSubview:ytView];
            [ytView show];
        }
        [areaView removeFromSuperview];
        [priceView removeFromSuperview];
        [moreView removeFromSuperview];
    }else{
        if (![rhytView superview]) {
            rhytView = [[RHYTView alloc] init];
            rhytView.delegate = self;
            [self.view addSubview:rhytView];
            [rhytView show];
        }
        [rhareaView removeFromSuperview];
        [rhpriceView removeFromSuperview];
        [rhmoreView removeFromSuperview];
    }
    self.tabBarController.tabBar.hidden = YES;
}

- (void)click4{
    [_tableView setContentOffset:CGPointMake(0, 275) animated:YES];
    NSString *str = [_dic objectForKey:@"tag"];
    if ([str isEqualToString:@"aaa"]) {
        if (![moreView superview]) {
            moreView = [[BHMoreView alloc] init];
            moreView.delegate = self;
            [self.view addSubview:moreView];
            [moreView show];
        }
        [priceView removeFromSuperview];
        [ytView removeFromSuperview];
        [areaView removeFromSuperview];
    }else{
        if (![rhmoreView superview]) {
            rhmoreView = [[RHMoreView alloc] init];
            rhmoreView.delegate = self;
            [self.view addSubview:rhmoreView];
            [rhmoreView show];
        }
        [rhareaView removeFromSuperview];
        [rhpriceView removeFromSuperview];
        [rhytView removeFromSuperview];
    }
    self.tabBarController.tabBar.hidden = YES;
}
//BHHouse
-(void)sendArea:(NSString *)text{
    NSLog(@"text:%@",text);
    [_bhDic setObject:text forKey:@"area"];
    [_bhText setObject:text forKey:@"area"];
    [_tableView.mj_header beginRefreshing];
    self.tabBarController.tabBar.hidden = NO;
}

-(void)sendPrice:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"价格由高到低"]) {
        [_bhDic setObject:@"high" forKey:@"price"];
    }else if ([text isEqualToString:@"价格由低到高"]){
        [_bhDic setObject:@"low" forKey:@"price"];
    }
    [_bhText setObject:text forKey:@"price"];
    [_tableView.mj_header beginRefreshing];
    self.tabBarController.tabBar.hidden = NO;
}

-(void)sendYT:(NSString *)text{
    NSLog(@"text:%@",text);
    [_bhDic setObject:text forKey:@"yt"];
    [_bhText setObject:text forKey:@"yt"];
    [_tableView.mj_header beginRefreshing];
    self.tabBarController.tabBar.hidden = NO;
}

-(void)sendMore:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"高层"]) {
        [_bhDic setObject:@"1" forKey:@"more"];
    }else if ([text isEqualToString:@"中层"]){
        [_bhDic setObject:@"2" forKey:@"more"];
    }else if ([text isEqualToString:@"底层"]){
        [_bhDic setObject:@"3" forKey:@"more"];
    }
    [_bhText setObject:text forKey:@"more"];
    [_tableView.mj_header beginRefreshing];
    self.tabBarController.tabBar.hidden = NO;
    
}
//RHHouse
-(void)sendRHArea:(NSString *)text{
    NSLog(@"text:%@",text);
    [_rhDic setObject:text forKey:@"area"];
    [_bhText setObject:text forKey:@"area"];
    [_tableView.mj_header beginRefreshing];
}

-(void)sendRHPrice:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"价格由高到低"]) {
        [_rhDic setObject:@"high" forKey:@"price"];
    }else if ([text isEqualToString:@"价格由低到高"]){
        [_rhDic setObject:@"low" forKey:@"price"];
    }
    [_bhText setObject:text forKey:@"price"];
    [_tableView.mj_header beginRefreshing];
}

-(void)sendRHYT:(NSString *)text{
    NSLog(@"text:%@",text);
    [_rhDic setObject:text forKey:@"yt"];
    [_bhText setObject:text forKey:@"yt"];
    [_tableView.mj_header beginRefreshing];
}

-(void)sendRHMore:(NSString *)text{
    NSLog(@"text:%@",text);
    if ([text isEqualToString:@"高层"]) {
        [_rhDic setObject:@"1" forKey:@"more"];
    }else if ([text isEqualToString:@"中层"]){
        [_rhDic setObject:@"2" forKey:@"more"];
    }else if ([text isEqualToString:@"低层"]){
        [_rhDic setObject:@"3" forKey:@"more"];
    }
    [_bhText setObject:text forKey:@"more"];
    [_tableView.mj_header beginRefreshing];
}

//头视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        
        if (array.count == 0) {
            
            return 0.0001f;
        }else
        {
            
             return  40.f;
        }
    }
    return 40.0;
}

- (void)loadRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadTag)];
    //
    [self.tableView.mj_header beginRefreshing];
    //根据位置设置半透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTag)];
    
}

- (void)loadTag{
    NSString *tag = [_dic objectForKey:@"tag"];
    if ([tag isEqualToString:@"aaa"]) {
        [self loadData];
    }else{
        [self loadData2];
    }
}

- (void)loadMoreTag{
    NSString *tag = [_dic objectForKey:@"tag"];
    if ([tag isEqualToString:@"aaa"]) {
        [self loadMoreData];
    }else{
        [self loadMoreData2];
    }
}

//加载更多
- (void)loadMoreData{
    [_tableView.mj_header endRefreshing];
    pageNo++;
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *longitude = [NSString stringWithFormat:@"%f",lng];
    NSString *latitude = [NSString stringWithFormat:@"%f",lat];
    [parmas setObject:longitude forKey:@"longitude"];
    [parmas setObject:latitude forKey:@"latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%li",pageNo] forKey:@"p"];
    
    NSString *area = [_bhDic objectForKey:@"area"];
    NSString *price = [_bhDic objectForKey:@"price"];
    NSString *yt = [_bhDic objectForKey:@"yt"];
    NSString *more = [_bhDic objectForKey:@"more"];
    if (area.length > 0) {
        [parmas setObject:area forKey:@"community"];
    }
    if (price.length > 0) {
        [parmas setObject:price forKey:@"price"];
    }
    if (yt.length > 0) {
        [parmas setObject:yt forKey:@"ealtype"];
    }
    if (more.length > 0) {
        [parmas setObject:more forKey:@"floor"];
    }
    
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Bridalchamber/chamber",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"买房res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [[responseObject objectForKey:@"result"] objectForKey:@"commend"];
            NSArray *arr2 = [[responseObject objectForKey:@"result"] objectForKey:@"list"];
            [array addObjectsFromArray:arr];
            [array2 addObjectsFromArray:arr2];
            [_tableView reloadData];
        }
        
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [_tableView.mj_footer endRefreshing];
    }];
    
}

- (void)loadMoreData2{
    pageNo++;
    [_tableView.mj_footer endRefreshing];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *longitude = [NSString stringWithFormat:@"%f",lng];
    NSString *latitude = [NSString stringWithFormat:@"%f",lat];
    [parmas setObject:longitude forKey:@"longitude"];
    [parmas setObject:latitude forKey:@"latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%li",pageNo] forKey:@"p"];
    
    NSString *area = [_rhDic objectForKey:@"area"];
    NSString *price = [_rhDic objectForKey:@"price"];
    NSString *yt = [_rhDic objectForKey:@"yt"];
    NSString *more = [_rhDic objectForKey:@"more"];
    
    if (area.length > 0) {
        [parmas setObject:area forKey:@"community"];
    }
    
    if (price.length > 0) {
        [parmas setObject:price forKey:@"price"];
    }
    
    if (yt.length > 0) {
        [parmas setObject:yt forKey:@"doormodel"];
    }
    
    if (more.length > 0) {
        [parmas setObject:more forKey:@"floor"];
    }
    
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Rentout/renthous",urlHead];
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"租房res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            [datas addObjectsFromArray:arr];
            NSLog(@"租房datas:%@",datas);
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    }];

}

//买房
- (void)loadData{
//    [SVProgressHUD show];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [_tableView.mj_footer endRefreshing];
    pageNo = 1;
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *longitude = [NSString stringWithFormat:@"%f",lng];
    NSString *latitude = [NSString stringWithFormat:@"%f",lat];
    [parmas setObject:longitude forKey:@"longitude"];
    [parmas setObject:latitude forKey:@"latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%li",pageNo] forKey:@"p"];
    
    NSString *area = [_bhDic objectForKey:@"area"];
    NSString *price = [_bhDic objectForKey:@"price"];
    NSString *yt = [_bhDic objectForKey:@"yt"];
    NSString *more = [_bhDic objectForKey:@"more"];
    if (area.length > 0) {
        [parmas setObject:area forKey:@"community"];
    }
    
    if (price.length > 0) {
        [parmas setObject:price forKey:@"price"];
    }
    
    if (yt.length > 0) {
        [parmas setObject:yt forKey:@"ealtype"];
    }
    
    if (more.length > 0) {
        [parmas setObject:more forKey:@"floor"];
    }
    
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Bridalchamber/chamber",urlHead];
    [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"买房res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [[responseObject objectForKey:@"result"] objectForKey:@"commend"];
            NSArray *arr2 = [[responseObject objectForKey:@"result"] objectForKey:@"list"];
            array = [arr mutableCopy];//推荐
            array2 = [arr2 mutableCopy];//不推荐
            [_tableView reloadData];
        }
        
        [_tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    }];
}
//租房
- (void)loadData2{
//    [SVProgressHUD show];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [_tableView.mj_footer endRefreshing];
    pageNo = 1;
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *longitude = [NSString stringWithFormat:@"%f",lng];
    NSString *latitude = [NSString stringWithFormat:@"%f",lat];
    [parmas setObject:longitude forKey:@"longitude"];
    [parmas setObject:latitude forKey:@"latitude"];
    [parmas setObject:[NSString stringWithFormat:@"%li",pageNo] forKey:@"p"];
    
    NSString *area = [_rhDic objectForKey:@"area"];
    NSString *price = [_rhDic objectForKey:@"price"];
    NSString *yt = [_rhDic objectForKey:@"yt"];
    NSString *more = [_rhDic objectForKey:@"more"];
    
    if (area.length > 0) {
        [parmas setObject:area forKey:@"community"];
    }
    
    if (price.length > 0) {
        [parmas setObject:price forKey:@"price"];
    }
    
    if (yt.length > 0) {
        [parmas setObject:yt forKey:@"doormodel"];
    }
    
    if (more.length > 0) {
        [parmas setObject:more forKey:@"floor"];
    }
    
    NSLog(@"parmas:%@",parmas);
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = [NSString stringWithFormat:@"%@/App/Rentout/renthous",urlHead];
    [manager GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"租房res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            datas = [arr mutableCopy];
            NSLog(@"租房datascount:%li",datas.count);
            [_tableView reloadData];
        }
        
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
        [_tableView.mj_header endRefreshing];
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
